import 'package:flutter/material.dart';
import 'package:flutter_app/basecomponent/OriginalListWidget.dart';
import 'package:flutter_app/util/AroutUtil.dart';
import 'package:flutter_app/util/DataUtil.dart';
import 'package:flutter_app/util/ToastUtil.dart';

import 'appwidget/app_list_widget.dart';
import 'customwidget/animal/BaseAnimation.dart';
import 'customwidget/animal/animat_list_widget.dart';
import 'customwidget/base_widget/BaseListWidget.dart';
import 'customwidget/container_widget/ContainerListWidget.dart';
import 'customwidget/event_widget/EventGrideWidget.dart';
import 'customwidget/function_widget/FunctionListWidget.dart';
import 'customwidget/layout_widget/LayoutListWidget.dart';
import 'customwidget/mixture_develper/fluttter_in_andriod.dart';
import 'customwidget/scroll_widget/ScrollListWidget.dart';
import 'net_data/NetDataWidget.dart';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  DateTime ?_lastPressedAdt; //上次点击时间 初始化的时候是空，下面会做判断
  int counter = 0;
  var _datas = DataUtil.getColumn();

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        WillPopScope(
            child: Text(""),
            onWillPop: () async {
              //当_lastPressedAdt 为空说明是第一次点击
              // 当Date.now().different(_lastPressAdt) 是比较当前时间和上一次点击的时间，如果差距小于1秒钟
              //当满足这两种情况的时候，说明这个可能是意外点击到了返回按钮，在这里我们返回flase，表示不响应
              if (_lastPressedAdt == null ||
                  DateTime.now().difference(_lastPressedAdt!) >
                      Duration(seconds: 1)) {
                //两次点击时间间隔超过1秒则重新计时
                _lastPressedAdt = DateTime.now();
                ToastUtil.toastShort("1秒钟连续两次点击返回键退出");
                return false;
              }

              return true;
            }),
        Container(
          padding: EdgeInsets.only(left: 10, top: 10, right: 10),
          child: GridView.builder(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                childAspectRatio: 1.5,
                crossAxisSpacing: 10,
                mainAxisSpacing: 10),
            itemBuilder: _buildItem,
            itemCount: _datas.length,
          ),
        ),
      ],
    );
  }
}

Widget _buildItem(BuildContext context, int index) {
  var _datas = DataUtil.getColumn();
  return GestureDetector(
    child: DecoratedBox(
      decoration: BoxDecoration(
          gradient:
              LinearGradient(colors: [Colors.red, Colors.orange[700]!]), //背景渐变
          borderRadius: BorderRadius.circular(3.0),
          boxShadow: [
            BoxShadow(
                color: Colors.black54,
                offset: Offset(2.0, 2.0),
                blurRadius: 4.0)
          ]),
      child: Container(
        alignment: Alignment.center,
        child: RichText(
          textAlign: TextAlign.center,
          text: TextSpan(
            text: _datas[index],
            style: TextStyle(fontSize: 20),
          ),
        ),
      ),
    ),
    onTap: () {
      toArount(index, context, _datas[index]);
    },
  );
}

void toArount(int index, BuildContext context, String title) {
  switch (index) {
    case 0:
      AroutUtil.goTo(context, BaseListWidget(text: title));
      break;
    case 1:
      AroutUtil.goTo(context, LayoutListWidget(text: title));
      break;
    case 2:
      AroutUtil.goTo(context, ContainerListWidget(text: title));
      break;
    case 3:
      AroutUtil.goTo(context, ScrollListWidget(text: title));
      break;
    case 4:
      AroutUtil.goTo(context, FunctionListWidget(text: title));
      break;
    case 5:
      AroutUtil.goTo(context, EventGrideWidget(title: title));
      break;
    case 6:
      AroutUtil.goTo(context, NetDataWidget(text: title));
      break;
    case 7:
      AroutUtil.goTo(context, AnimatListWidget(text: title));
      break;
    case 8:
      AroutUtil.goTo(context, OriginalListWidget(text: title));
      break;
    case 9:
      AroutUtil.goTo(context, FlutterInAndroid(text: title));
      break;
    case 10:
      AroutUtil.goTo(context, AppListWidget(text: title));
      break;
    case 11:
      AroutUtil.goTo(context, AppListWidget(text: title));
      break;
  }
}
